﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for MessagesDAL
/// </summary>
public class MessagesDAL: StandardConnectDB, IMessages
{
	public MessagesDAL()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    public List<Messages> GetMessagessOfUser(Users user)
    {
        List<Messages> listMesg = new List<Messages>();


        Dictionary<string, object> paramist = new Dictionary<string, object>();
        paramist.Add("pUserName", user.Username);

        DataTable dt = this.DTExecuteSelect("spGetMessagessOfUser", paramist);

        foreach (DataRow dr in dt.Rows)
        {
            listMesg.Add(this.ConvertObjectDataRowToObjectMessages(dr));
        }
        
          
        return listMesg;
    }


    public Messages GetMessagesUser(Users user)
    {
        Messages ms = new Messages();
        return ms;
    }



    public List<Messages> GetMessagessOfUserByDate(Users user, DateTime date)
    {
        List<Messages> listms = new List<Messages>();
        
        Dictionary<string, object> paramist = new Dictionary<string, object>();



        throw new NotImplementedException();
    }

    public bool CreateMessage(Users fromUser, Users toUser, string subject, string contents)
    {
        bool kq = false;
        Dictionary<string, object> paramist = new Dictionary<string, object>();
        
        try
        {
            paramist.Add("@pFromUser", fromUser.Username);
            paramist.Add("@pToUserName", toUser.Username);
            paramist.Add("@pContents", contents);
            paramist.Add("@pSubject", subject);
           kq = this.ExecuteUpdate("spCreateMessage", paramist);
        }
        catch
        {
            kq = false;
        }
      return kq;
    }

    public bool DeleteMessage(Messages message)
    {
        throw new NotImplementedException();
    }

    public Messages ConvertObjectDataRowToObjectMessages(DataRow dr)
    {
        
        Messages Ms = new Messages();

        string username = dr["FromUser"].ToString();
        IUsers iUsers = new UsersDAL();
        Ms.FromUser = iUsers.GetUserByUsername(username);

        string username1 = dr["ToUserName"].ToString();
        IUsers iUser = new UsersDAL();
        Ms.ToUser = iUser.GetUserByUsername(username1);

      
        Ms.Subject = dr["Subject"].ToString();
        Ms.Contents = dr["Contents"].ToString();
        Ms.CreateTime = Convert.ToDateTime(dr["CreateTime"].ToString());
        return Ms;

    }
}